import numpy as np

glass_data = {
    "BK7": [
        300, 1.5527702635739, 322, 1.5458699289209, 344, 1.5404466868331,
        366, 1.536090527917, 388, 1.53252773217, 410, 1.529568767224,
        432, 1.5270784291406, 454, 1.5249578457324, 476, 1.5231331738499,
        498, 1.5215482528369, 520, 1.5201596882463, 542, 1.5189334783109,
        564, 1.5178426478869, 586, 1.516865556749, 608, 1.5159846691816,
        630, 1.5151856452759, 652, 1.5144566604975, 674, 1.513787889767,
        696, 1.5131711117948, 718, 1.5125994024544, 740, 1.5120668948646,
        762, 1.5115685899969, 784, 1.5111002059336, 806, 1.5106580569705,
        828, 1.5102389559626, 850, 1.5098401349174, 872, 1.5094591800239,
        894, 1.5090939781792, 916, 1.5087426727363],
    "BAF10": [
        350, 1.7126880848268, 371, 1.7044510025682, 393, 1.6978539633931,
        414, 1.6924597573902, 436, 1.6879747521657, 457, 1.6841935148947,
        479, 1.6809676313681, 500, 1.6781870617363, 522, 1.6757684467878,
        543, 1.6736474831891, 565, 1.6717737892968, 586, 1.6701073530462,
        608, 1.6686160168249, 629, 1.6672736605352, 651, 1.6660588657981,
        672, 1.6649539185393, 694, 1.6639440538738, 715, 1.6630168772865,
        737, 1.6621619159417, 758, 1.6613702672977, 780, 1.6606343213443,
        801, 1.6599475391478, 823, 1.6593042748862, 844, 1.6586996317841,
        866, 1.6581293446924, 887, 1.6575896837763, 909, 1.6570773750475],
    "FK51A": [
        290, 1.5145777204082, 312, 1.5092112868865, 334, 1.5049961987453,
        356, 1.5016153970446, 378, 1.4988558885761, 400, 1.496569610433,
        422, 1.4946506898002, 444, 1.4930216011953, 466, 1.4916244098644,
        488, 1.49041505042, 511, 1.4893594837084, 533, 1.4884310526027,
        555, 1.4876086240083, 577, 1.486875258765, 599, 1.486217243501,
        621, 1.4856233753353, 643, 1.4850844262039, 665, 1.4845927367446,
        687, 1.484141904927, 709, 1.483726544853, 732, 1.4833420981287,
        754, 1.4829846850495, 776, 1.482650986233, 798, 1.4823381477539,
        820, 1.4820437045732, 842, 1.4817655183243, 864, 1.481501726448,
        886, 1.4812507003621, 908, 1.4810110108734],
    "LASF9": [
        370, 1.9199725545705, 391, 1.9057858245373, 412, 1.8945401582481,
        433, 1.8854121949451, 455, 1.877863643024, 476, 1.8715257028176,
        497, 1.8661362648008, 519, 1.8615034773283, 540, 1.8574834752011,
        561, 1.8539661699122, 583, 1.8508658556, 604, 1.8481148099285,
        625, 1.8456588222442, 646, 1.8434539988324, 668, 1.8414644361915,
        689, 1.8396604975285, 710, 1.8380175167434, 732, 1.8365148106821,
        753, 1.8351349171703, 774, 1.8338630007484, 796, 1.8326863845545,
        817, 1.8315941782006, 838, 1.8305769794709, 859, 1.8296266333424,
        881, 1.8287360359155, 902, 1.8278989738228],
    "SF5": [
        370, 1.7286549847245, 391, 1.7170151864402, 412, 1.7079037179421,
        433, 1.7005724270177, 455, 1.6945472844297, 476, 1.6895110487297,
        497, 1.685242265691, 519, 1.6815810964, 540, 1.678409006027,
        561, 1.6756360973958, 583, 1.6731928929908, 604, 1.6710248234743,
        625, 1.6690884260039, 646, 1.6673486579281, 668, 1.6657769585173,
        689, 1.6643498246044, 710, 1.6630477468358, 732, 1.6618544037398,
        753, 1.6607560432197, 774, 1.6597410023473, 796, 1.6587993305922,
        817, 1.6579224913632, 838, 1.6571031234995, 859, 1.6563348491305,
        881, 1.6556121177295, 902, 1.654930078671],
    "SF10": [
        380, 1.7905788948419, 401, 1.7776074571692, 422, 1.7673620572474,
        443, 1.7590649148507, 464, 1.7522127524444, 486, 1.7464635698826,
        507, 1.741575877046, 528, 1.7373738218659, 549, 1.7337260730259,
        570, 1.7305324562829, 592, 1.7277151818026, 613, 1.7252129043714,
        634, 1.7229765939984, 655, 1.7209665988467, 676, 1.719150514229,
        698, 1.7175016091415, 719, 1.7159976462946, 740, 1.7146199848831,
        761, 1.7133528897994, 782, 1.7121829937648, 804, 1.7110988742233,
        825, 1.7100907173852, 846, 1.7091500491754, 867, 1.7082695180523,
        888, 1.7074427184169, 910, 1.7066640460471],
    "SF11": [
        370, 1.8700216173234, 391, 1.8516255860581, 412, 1.8374707714715,
        433, 1.8262323798466, 455, 1.8170946940119, 476, 1.8095242343848,
        497, 1.803155581666, 519, 1.7977291183308, 540, 1.7930548640505,
        561, 1.7889903663666, 583, 1.7854266026774, 604, 1.7822786683156,
        625, 1.7794794394722, 646, 1.7769751487395, 668, 1.7747222267051,
        689, 1.7726850031375, 710, 1.770834004936, 732, 1.7691446766161,
        753, 1.7675964052635, 774, 1.7661717683505, 796, 1.764855947008,
        817, 1.7636362637211, 838, 1.7625018146862, 859, 1.7614431749629,
        881, 1.7604521601554, 902, 1.7595216323879],
    "Diamond": [
        309.960469, 2.523632303, 326.274177, 2.505813241,
        344.400521, 2.487866556, 364.659375, 2.473323675,
        387.450586, 2.464986815, 413.280625, 2.455051934,
        442.800669, 2.441251728, 476.862259, 2.431478974,
        516.600781, 2.427076431, 563.564488, 2.420857286,
        619.920937, 2.411429037, 688.801041, 2.406543164,
        774.901171, 2.406202402, 885.601339, 2.400035416],
    "Ice": [
        350.0, 1.3249, 390.0, 1.3203, 400.0, 1.3194, 410.0, 1.3185,
        420.0, 1.3177, 430.0, 1.3170, 440.0, 1.3163, 450.0, 1.3157,
        460.0, 1.3151, 470.0, 1.3145, 480.0, 1.3140, 490.0, 1.3135,
        500.0, 1.3130, 510.0, 1.3126, 520.0, 1.3121, 530.0, 1.3117,
        540.0, 1.3114, 550.0, 1.3110, 560.0, 1.3106, 570.0, 1.3103,
        580.0, 1.3100, 590.0, 1.3097, 600.0, 1.3094, 610.0, 1.3091,
        620.0, 1.3088, 630.0, 1.3085, 640.0, 1.3083, 650.0, 1.3080,
        660.0, 1.3078, 670.0, 1.3076, 680.0, 1.3073, 690.0, 1.3071,
        700.0, 1.3069, 710.0, 1.3067, 720.0, 1.3065, 730.0, 1.3062,
        740.0, 1.3060, 750.0, 1.3059, 760.0, 1.3057, 770.0, 1.3055,
        780.0, 1.3053, 790.0, 1.3051, 800.0, 1.3049, 810.0, 1.3047,
        820.0, 1.3046, 830.0, 1.3044],
    "Quartz": [
        307.4, 1.4857914366574, 318.2, 1.4831504333467, 329.4, 1.4807144415912, 341.0, 1.4784676522483,
        353.0, 1.4763951298847, 365.5, 1.4744682820342, 378.3, 1.4727046797948, 391.7, 1.4710525123802,
        405.5, 1.4695286500209, 419.7, 1.4681218218832, 434.5, 1.4668048202486, 449.8, 1.465580829975,
        465.7, 1.4644360310913, 482.1, 1.4633719346282, 499.1, 1.4623764385944, 516.7, 1.4614449911601,
        534.9, 1.4605730794883, 553.7, 1.4597562854765, 573.2, 1.4589865613939, 593.4, 1.4582607881284,
        614.3, 1.4575758068817, 636.0, 1.4569256013294, 658.4, 1.4563104080175, 681.6, 1.4557246986958,
        705.6, 1.4551660299221, 730.5, 1.4546298754538, 756.2, 1.4541161650842, 782.9, 1.4536188536247,
        810.4, 1.4531396102638, 839.0, 1.4526712743322],
    "Salt": [
        330.1, 1.5901770529305, 347.1, 1.5831035268784, 364.9, 1.5769335436982, 383.6, 1.5715104898095,
        403.4, 1.5666930945141, 424.1, 1.5624559698771, 445.9, 1.5586908997915, 468.8, 1.5553465430315,
        492.9, 1.552364453562, 518.2, 1.5497071165934, 544.8, 1.5473314531459, 572.8, 1.5452021551566,
        602.2, 1.5432958337696, 633.2, 1.5415799641644, 665.7, 1.5400424900868, 699.9, 1.5386578582517,
        735.9, 1.537409227939, 773.7, 1.5362847064265, 813.5, 1.5352679023069],
    "Sapphire": [
        357.0, 1.7954050494599, 368.7, 1.792654904549, 380.7, 1.7901123299044, 393.2, 1.7877228296252,
        406.0, 1.7855131978094, 419.3, 1.7834376078764, 433.0, 1.7815032886078, 447.2, 1.7796876970942,
        461.8, 1.7779954669163, 477.0, 1.7763969806669, 492.6, 1.7749069668851, 508.7, 1.7735082113872,
        525.3, 1.7721946418157, 542.5, 1.7709534510436, 560.2, 1.7697870034397, 578.6, 1.768678092594,
        597.5, 1.7676347594555, 617.0, 1.7666467187031, 637.2, 1.7657055418929, 658.1, 1.7648083776856,
        679.6, 1.7639560666816, 701.8, 1.7631412029905, 724.8, 1.7623575309256, 748.5, 1.7616058134504,
        773.0, 1.7608801757554, 798.3, 1.7601781977445, 824.4, 1.7594974153468, 851.3, 1.7588353492177]
}

glass_basis = [656.27, 587.56, 486.13]


def solve_cauchy_coeff(name, interleaved):
    lambdas = np.array(interleaved[0::2])
    indices = np.array(interleaved[1::2])
    assert lambdas.shape == indices.shape
    inv_ll = 1.0 / (lambdas * lambdas)
    m = np.array([np.ones_like(lambdas), inv_ll, inv_ll * inv_ll]).transpose()
    c = np.linalg.lstsq(m, indices, rcond=None)[0]
    print(f"Error ({name}): {m @ c - indices}")
    basis = np.array(glass_basis)
    inv_bb = 1.0 / (basis * basis)
    m = np.array([np.ones_like(basis), inv_bb, inv_bb * inv_bb]).transpose()
    return m @ c


if __name__ == "__main__":
    c = {name: solve_cauchy_coeff(name, values) for name, values in glass_data.items()}
    for name, ior in c.items():
        print(f'make_desc("{name.lower()}", make_float3({ior[0]}f, {ior[1]}f, {ior[2]}f)),')
